<template>
    <div class="happy-input"
         :class="{'happy-input_suffix': suffix}"
    >
        <input class="happy-input_inner"
                :placeholder="placeholder"
                :type="showPassword ? (passwordVisible ? 'text' : 'password') : type"
                :disabled="disabled"
                :class="{
                    'is-disabled': disabled
                }"
               :value="value"
               @input="handleInput"
               @blur="handleBlur"
        />
        <span class="happy-input_suffix">
           <i class="happy-input_icon happy-icon-cancel" v-if="clearable && value" @click="clear"></i>
           <i class="happy-input_icon happy-icon-visible" v-if="showPassword && passwordVisible" @click="handlePassword"></i>
           <i class="happy-input_icon happy-icon-vision" v-if="showPassword && !passwordVisible" @click="handlePassword"></i>
         </span>
    </div>
</template>

<script>
    export default {
        name: "happyInput",
        props:{
            placeholder:{
                type: String,
                default: ''
            },
            type:{
                type: String,
                default: 'text'
            },
            disabled:{
                type: Boolean,
                default: false
            },
            value: String,
            clearable:{
                type: Boolean,
                default: false
            },
            showPassword:{
                type: Boolean,
                default: false
            }
        },
        data(){
            return {
                passwordVisible: false
            }
        },
        computed:{
            suffix(){
                return this.showPassword || this.clearable
            }
        },
        methods:{
            handleInput(e){
                this.$emit('input', e.target.value)
            },
            clear(){
                this.$emit('clear');
            },
            handlePassword(){
                if (this.showPassword) this.passwordVisible = !this.passwordVisible;
                // this.passwordVisible ? this.$emit('click', 'password') : this.$emit('click', 'text');
            },
            handleBlur(e){
                this.$emit('blur', e)
            }
        }
    }
</script>

<style lang="scss" scoped>
    .happy-input{
        width: 100%;
        position: relative;
        font-size: 14px;
        display: inline-block;
        .happy-input_inner{
            -webkit-appearance: none;
            background-color: #fff;
            background-image: none;
            border: 1px solid #dcdfe6;
            border-radius: 4px;
            box-sizing: border-box;
            color: #606266;
            display: inline-block;
            font-size: inherit;
            height: 40px;
            line-height: 40px;
            outline: none;
            padding: 0 15px;
            transition: border-color .2s cubic-bezier(.645,045,.355,1);
            width: 100%;

            &:focus{
                outline: none;
                border-color: #409eff;
            }
            // input禁用样式
            &.is-disabled{
                background-color: #f5f7fa;
                border-color: #e4e7ed;
                color: #c0c4cc;
                cursor:not-allowed;
            }
        }
    }
    // 后面加suffix的意思是后面如果有后缀的话，触发该样式
    .happy-input_suffix{
        .happy-input_inner{
            padding-right: 30px;
        }
        .happy-input_suffix{
            position: absolute;
            right: 10px;
            height: 100%;
            top: 0;
            line-height: 40px;
            text-align: center;
            color: #c0c4cc;
            transition: all .3s;
            z-index: 900;
            i{
                color: #c0c4cc;
                font-size: 14px;
                cursor: pointer;
                transition: color .2s cubic-bezier(.645,.045,.355,1);
            }
        }
    }
</style>

